package com.powergisol.gis.service.impl;

import com.powergisol.gis.Enum.ServiceTypeEnum;
import com.powergisol.gis.dao.*;
import com.powergisol.gis.entity.LatitudeLongitude;
import com.powergisol.gis.entity.TabLayer;
import com.powergisol.gis.entity.TabSpace;
import com.powergisol.gis.service.TabSceneDrawService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabLayerVO;
import com.powergisol.gis.vo.TabSceneDrawVo;
import com.powergisol.gis.vo.TabServiceVo;
import com.powergisol.gis.vo.TabUserVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TabSceneDrawServiceImpl implements TabSceneDrawService {
    @Resource
    private TabDataSettingMapper tabDataSettingMapper;
    @Resource
    private TabSceneDrawMapper sceneDrawMapper;
    @Resource
    private TabLayerMapper layerMapper;
    @Resource
    private TabTreeMapper treeMapper;
    @Resource
    private TabSpaceMapper spaceMapper;
    @Resource
    private TabDrawStyleMapper drawStyleMapper;

    @Value("${earth-url}")
    private String earthUrl;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result saveSceneDraw(TabSceneDrawVo vo, TabUserVo tabUserVo) {
        Result result = new Result();
        int countByUuid = sceneDrawMapper.getCountByUuid(vo.getUuid());
        if (countByUuid>0) {
            return result.failure(1,"请勿添加相同数据图形绘制");
        }
        if (vo.getDrawName().isEmpty() || vo.getDrawName() == null) {
            int count = sceneDrawMapper.getCountBySceneId(vo.getSceneId(), vo.getDrawType());
            StringBuffer buffer = new StringBuffer();
            count = count + 1;
            switch (vo.getDrawType()) {
                case 1:
                    vo.setDrawName(buffer.append("点_" + count).toString());
                    break;
                case 2:
                    vo.setDrawName(buffer.append("线_" + count).toString());
                    break;
                case 3:
                    vo.setDrawName(buffer.append("面_" + count).toString());
                    break;
                case 5:
                    vo.setDrawName(buffer.append("圆_" + count).toString());
                    break;
                default:
                    break;
            }
        }
        //流程---> 发布服务、新增图层、新增图层树、绘制信息、经纬度、样式风格
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        String serviceName = vo.getDrawName() + sdf1.format(new Date());
        //发布服务
        TabServiceVo tabServiceVo = saveService(vo, tabUserVo, serviceName);
//        Integer serviceId = saveService(vo, tabUserVo, serviceName);
        //获取该场景id的信息
        Integer getTreeIdBySceneId = treeMapper.getBySceneId(vo.getSceneId());

        //获取发布的服务url和id
//        TabServiceVo urlByServiceId = tabDataSettingMapper.getUrlByServiceId(serviceId);
        //转换drawType
        Integer layerType = getLayerType(vo.getDrawType());

        TabLayerVO returnLayer = new TabLayerVO();
        returnLayer.setLayerName(vo.getDrawName());
        returnLayer.setLayerType(layerType);
        returnLayer.setServiceName(serviceName);
        returnLayer.setServiceUrl(tabServiceVo.getServiceUrl());
        returnLayer.setServiceId(tabServiceVo.getId());
        returnLayer.setSceneId(vo.getSceneId());
        returnLayer.setTreeId(getTreeIdBySceneId);
        returnLayer.setServiceType(layerType);
        returnLayer.setUuid(vo.getUuid());
//        //新增图层
//        Integer layerId = saveLayer(vo, urlByServiceId, vo.getDrawName());
//
//        //如果该场景id有对应的id  则当作父级id添加新增的图层信息
//        if (getTreeIdBySceneId != null) {
//            //图层树 实体
//            TabTree tree = new TabTree();
//            tree.setpId(getTreeIdBySceneId);
//            tree.setLayerId(layerId);
//            tree.setTreeName(vo.getDrawName());
//            tree.setIsLayer(1);//1 代表是图层
//            //新增图层树
//            treeMapper.saveTree(tree);
//        }

        //保存绘制信息
        vo.setUuid(vo.getUuid());
        sceneDrawMapper.insertSelective(vo);

        //经纬度保存
        vo.getLatitudeLongitude().forEach(e -> {
            TabSpace tabSpace = new TabSpace();
            tabSpace.setLat(e.getLatitude());
            tabSpace.setLon(e.getLongitude());
            tabSpace.setDrawId(vo.getId());
            tabSpace.setElevation(e.getElevation());
            spaceMapper.insertSelective(tabSpace);
        });

        //样式风格
        vo.getDrawStyle().setDrawId(vo.getId());
        drawStyleMapper.insertSelective(vo.getDrawStyle());

        result.setData(returnLayer);
        return result;
    }

    @Override
    public Result selectSceneDraw(Integer id) {
        //绘制信息
        TabSceneDrawVo vo = sceneDrawMapper.selectSceneDraw(id);
        if (vo.getId() == null) {
            return new Result().failure(1, "该图层未有详细信息");
        }
        //经纬度
        List<LatitudeLongitude> latitudeLongitudes = spaceMapper.selectByDrawId(vo.getId());
        vo.setLatitudeLongitude(latitudeLongitudes);
        //风格样式
        vo.setDrawStyle(drawStyleMapper.selectByDrawId(vo.getId()));
        return new Result().success(vo);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result updateSceneDraw(TabSceneDrawVo sceneDrawVo) {
        //修改场景绘制
        sceneDrawMapper.updateByPrimaryKeySelective(sceneDrawVo);
//        //修改图层
//        TabLayer layer = new TabLayer();
//        layer.setId(sceneDrawVo.getLayerId());
//        layer.setLayerName(sceneDrawVo.getDrawName());
//        layerMapper.updateLayerNameLayerByDrawName(layer);
//        //修改树
//        TabTree tree = new TabTree();
//        tree.setLayerId(sceneDrawVo.getLayerId());
//        tree.setTreeName(sceneDrawVo.getDrawName());
//        treeMapper.upTreeNameByLayerId(tree);
        //修改风格样式

        if (null != sceneDrawVo.getLatitudeLongitude()) {
            List<LatitudeLongitude> latitudeLongitude = sceneDrawVo.getLatitudeLongitude();
            latitudeLongitude.forEach(l -> {
                TabSpace tabSpace = new TabSpace();
                tabSpace.setId(l.getId());
                tabSpace.setLon(l.getLongitude());
                tabSpace.setLat(l.getLatitude());
                tabSpace.setElevation(l.getElevation());
                spaceMapper.updateByPrimaryKey(tabSpace);
            });
        }
        if (null != sceneDrawVo.getDrawStyle()) {
            if (null != sceneDrawVo.getDrawStyle().getId()) {
                drawStyleMapper.updateByPrimaryKeySelective(sceneDrawVo.getDrawStyle());
            }
        }

        return new Result().success("修改成功");
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result deleteSceneDraw(Integer id) {
        //获取需要删除的场景绘制信息
        if (id != null) {
            TabSceneDrawVo vo = sceneDrawMapper.selectSceneDraw(id);

            if (null != vo) {
                //删除场景绘制
                sceneDrawMapper.deleteByPrimaryKey(vo.getId());
                //删除风格样式
                drawStyleMapper.deleteByDrawId(vo.getId());
                //删除图层
                layerMapper.deleteLayerById(vo.getLayerId());
                //删除服务
                tabDataSettingMapper.delTabService(vo.getServiceId());
                //删除服务中间表
                tabDataSettingMapper.delUserService(vo.getServiceId());
                //删除树
                treeMapper.delTreeByLayerId(vo.getLayerId());
            }
            return new Result().success("删除成功");
        }

        return new Result().failure(1, "删除失败");
    }

    /**
     * 转换drawType
     *
     * @param drawType
     * @return
     */
    public Integer getLayerType(Integer drawType) {
        Integer layerType = null;
        switch (drawType) {
            case 1:
                layerType = ServiceTypeEnum.DXMDATA.getValue();
                break;
            case 2:
                layerType = ServiceTypeEnum.XIANDATA.getValue();
                break;
            case 3:
                layerType = ServiceTypeEnum.MIANDATA.getValue();
                break;
            case 5:
                layerType = ServiceTypeEnum.ROUND.getValue();
        }
        return layerType;

    }

    /**
     * 新增图层
     *
     * @param vo
     * @param serviceVo
     * @return
     */
    public Integer saveLayer(TabSceneDrawVo vo, TabServiceVo serviceVo, String serviceName) {
        //新增图层实体类
        TabLayer layer = new TabLayer();
        layer.setServiceId(serviceVo.getId());
        layer.setServiceUrl(serviceVo.getServiceUrl());
        layer.setLayerName(serviceName);
        layer.setLayerType(ServiceTypeEnum.DXMDATA.getValue());//13 为 点、线、面自定义类型

        layerMapper.saveLayer(layer);

        return layer.getId();
    }

    /**
     * 发布服务
     *
     * @param vo
     * @return
     */
    public TabServiceVo saveService(TabSceneDrawVo vo, TabUserVo tabUserVo, String serviceName) {
        //发布服务
        TabServiceVo tabServiceVo = new TabServiceVo();
        tabServiceVo.setServiceUrl(earthUrl + serviceName);

        //根据用户查询用户
        tabServiceVo.setUserId(tabUserVo.getId());

        //服务必填信息
        tabServiceVo.setName(serviceName);
        tabServiceVo.setReleaseTime(new Date());
        //转换drawType
        Integer layerType = getLayerType(vo.getDrawType());
        tabServiceVo.setServiceType(layerType);
        tabServiceVo.setUuid(vo.getUuid());
        tabServiceVo.setState(1);
        //储存服务信息
        tabDataSettingMapper.saveService(tabServiceVo);
        tabDataSettingMapper.saveUserService(tabServiceVo.getUserId(), tabServiceVo.getId());

        return tabServiceVo;
    }


}
